package com.hardware.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hardware.common.R;
import com.hardware.entity.Supplier;
import com.hardware.entity.category.SupplierCategory;
import com.hardware.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.hardware.utils.RedisConstants.*;

@Slf4j
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = CACHE_SUPPLIER_KEY,key = "#page +'_'+ #pageSize")
    public R<Page> page(int page, int pageSize, String name){

        return supplierService.page(page,pageSize,name);

    }

    /**
     * 根据id查询供应商
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(value = CACHE_SUPPLIER_KEY,key = "#id")
    public R<Supplier> get(@PathVariable Long id){
        Supplier supplier = supplierService.getById(id);
        return R.success(supplier);
    }

    /**
     * 新增供应商
     * @param supplier
     * @return
     */
    @PostMapping
    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> save(@RequestBody Supplier supplier){
        log.info("supplier {}",supplier);
        supplierService.save(supplier);
        return R.success("新增供应商成功");
    }

    /**
     * 修改供应商状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> updateByid(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<Supplier> lqw = new LambdaQueryWrapper<>();
        lqw.in(Supplier::getId,ids);
        List<Supplier> collect = ids.stream().map((item) -> {
            Supplier supplier = new Supplier();
            supplier.setStatus(status);
            supplier.setId(item);
            return supplier;
        }).collect(Collectors.toList());
        supplierService.updateBatchById(collect);
        return R.success("更新成功");
    }

    /**
     * 修改供应商
     * @param supplier
     * @return
     */
    @PutMapping
    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> update(@RequestBody Supplier supplier){

        supplierService.updateById(supplier);

        return R.success("修改供应商成功");
    }

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = CACHE_SUPPLIER_KEY,allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){

        supplierService.removeByIds(ids);

        return R.success("供应商数据删除成功");
    }

    /**
     * 根据条件查询供应商
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "cache:supplier:list")
    public R<List<Supplier>> list(){
        //条件构造器
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Supplier::getUpdateTime);

        List<Supplier> list = supplierService.list(queryWrapper);
        return R.success(list);
    }
}
